/**
 * 初始化收货人信息详情对话框
 */
var ConsigneeInfoDlg = {
    consigneeInfoData : {},
    validateFields: {
	 consigneeName: {
	        validators: {
	            notEmpty: {
	                message: '收货人不能为空'
	            }
	        }
	    },
	    consigneeMobile: {
	        validators: {
	            notEmpty: {
	                message: '手机号不能为空'
	            }
	        }
	    },
	    consigneePlace: {
	        validators: {
	            notEmpty: {
	                message: '收货人地址不能为空'
	            }
	        }
	    }
    }
};

/**
 * 验证数据是否为空
 */
ConsigneeInfoDlg.validate = function () {
    $('#consigneeForm').data("bootstrapValidator").resetForm();
    $('#consigneeForm').bootstrapValidator('validate');
    return $("#consigneeForm").data('bootstrapValidator').isValid();
};


/**
 * 清除数据
 */
ConsigneeInfoDlg.clearData = function() {
    this.consigneeInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ConsigneeInfoDlg.set = function(key, val) {
    this.consigneeInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ConsigneeInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
ConsigneeInfoDlg.close = function() {
    parent.layer.close(window.parent.Consignee.layerIndex);
}

/**
 * 收集数据
 */
ConsigneeInfoDlg.collectData = function() {
    this
    .set('id')
    .set('consigneeId')
    .set('consigneeName')
    .set('consigneeGender')
    .set('consigneeCard')
    .set('consigneePlace')
    .set('consigneeTell')
    .set('consigneeMobile')
    .set('consigneeCompanyName')
    .set('consigneeCompanyPlace')
    .set('consigneeCompanyTell')
    .set('consigneeRememberId');
}

/**
 * 提交添加
 */
ConsigneeInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();
   
    if (!this.validate()) {
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/consignee/add", function(data){
        Feng.success("添加成功!");
        window.parent.Consignee.table.refresh();
        ConsigneeInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.consigneeInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
ConsigneeInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();
   
    if (!this.validate()) {
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/consignee/update", function(data){
        Feng.success("修改成功!");
        window.parent.Consignee.table.refresh();
        ConsigneeInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.consigneeInfoData);
    ajax.start();
}

$(function() {
	//初始化性别选项
    $("#consigneeGender").val($("#sexValue").val());
	 Feng.initValidator("consigneeForm", ConsigneeInfoDlg.validateFields);
});
